<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          v-hasPermi="['purchase:lqmReport:query']"
          @click="handleSearch"
        >
          搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['purchase:lqmReport:export']"
        >
          导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="lqmMstrList"
      id="tabLqmReport"
      height="calc(100% - 100px)"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" width="40" type="index" align="center" />
      <el-table-column
        label="请购单号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="lqmDjbh"
      />
      <el-table-column
        label="需求日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="lqmXqrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.lqmXqrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="需求部门"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="lqmBmdm"
      >
        <template v-slot="scope">
          <dict-tag :options="deptList" :value="scope.row.lqmBmdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        min-width="220"
        align="center"
        prop="lqmPmgg"
      />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="lqmDw"
      >
        <template v-slot="scope">
          <dict-tag :options="unitOptions" :value="scope.row.lqmDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="请购数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="lqmQgsl"
      />
      <el-table-column
        label="参考单价"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="lqmCkdj"
      />
      <el-table-column
        label="申请人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="lqmJlry"
      />
      <el-table-column
        label="申请日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="lqmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.lqmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="需求日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="lqmPmgg">
            <el-input
              v-model="queryParams.lqmPmgg"
              placeholder="请输入品名规格"
              clearable
              size="small"
              type="textarea"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { exportLqmReport, lqmReportList } from "@/api/purchase/lqmMstr";
import { listDept } from "@/api/system/dept";

export default {
  name: "LqmReport",
  data() {
    return {
      tableH: document.body.clientHeight - 300,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 零星请购单表格数据
      lqmMstrList: [],
      // 弹出层标题
      title: "",
      // 建立日期时间范围
      dateRange: [
        this.$moment().startOf("month").toDate(),
        new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 60),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        lqmPmgg: null,
      },
      // 表单参数
      form: {},
      unitOptions: [],
      deptList: [],
    };
  },
  created() {
    //获取全部单位
    this.getAllDicts("sys_unit").then((response) => {
      this.unitOptions = response.data;
    });
    // 获取全部部门
    listDept().then((res) => {
      this.deptList = res.data;
      for (let item of this.deptList) {
        item.dictLabel = item.deptName;
        item.listClass = "default";
        item.dictValue = item.deptId + "";
      }
    });
    this.getList();
  },
  methods: {
    /** 查询零星请购单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginLqmJlrq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endLqmJlrq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      lqmReportList(this.queryParams).then((response) => {
        this.lqmMstrList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        lqmReportList(this.queryParams).then((response) => {
          this.lqmMstrList = this.lqmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSearch() {
      this.title = "查询零星请购单";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否导出所有待零星采购明细？", "确认导出", {
        cancelButtonText: "取消",
        confirmButtonText: "确认",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportLqmReport(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
